package com.z.redis.channel.select;

import com.z.redis.channel.LocalChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.util.Objects;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class NioSelectChannelOption implements LocalChannelOption {
    private final NioEventLoopGroup boss;
    private final NioEventLoopGroup workers;

    public NioSelectChannelOption(NioEventLoopGroup boss, NioEventLoopGroup workers) {
        this.boss = Objects.requireNonNull(boss, "boss group cannot be null");
        this.workers = Objects.requireNonNull(workers, "workers group cannot be null");
    }
    public NioSelectChannelOption() {
        this.boss = new NioEventLoopGroup(4, new ThreadFactory() {
            private AtomicInteger index = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "Server_boss_" + index.getAndIncrement());
            }
        });
        this.workers = new NioEventLoopGroup(8, new ThreadFactory() {
            private AtomicInteger index = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "Server_workers_" + index.getAndIncrement());
            }
        });
    }
    @Override
    public EventLoopGroup boss() { return this.boss; }
    @Override
    public EventLoopGroup workers() { return this.workers; }
    @Override
    public Class getChannelClass(){
        return NioServerSocketChannel.class;
    }
}
